package com.ssm.controller;

import com.ssm.po.Admin;
import com.ssm.po.StuVo;
import com.ssm.po.Student;
import com.ssm.service.clasService;
import com.ssm.service.instituteService;
import com.ssm.service.stuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    private stuService stuService;

    @Autowired
    private clasService clasService;

    @Autowired
    private instituteService instituteService;
    @RequestMapping("showSearch")
    public String showSearch(HttpServletRequest request, Model model){
        Admin admin = (Admin) request.getSession(true).getAttribute("admin");
        if (admin != null){
            request.getSession(true).setAttribute("i_show", instituteService.show());
            request.getSession(true).setAttribute("c_show",clasService.show());
            return "stu_searchandmanage";
        }else {
            model.addAttribute("msg","登录信息已失效,请重新登录");
            return "index";
        }
    }

    @RequestMapping("doSearch")
    public String doSearch(Student student, Model model){
        List<Student> students =stuService.doSearch(student);
            model.addAttribute("students",students);
            return "forward:/stu/showSearch";
    }

    @RequestMapping("doDel")
    public String doDel(Integer[] ids){
        for (Integer id : ids) {
            stuService.doDel(id);
        }
        return "redirect:/stu/doSearch";
    }

    @RequestMapping("doIns")
    public String doIns(Student student,HttpServletRequest request){
        if(stuService.check(student)!=null){
            request.getSession(true).setAttribute("msg","id重复,请检查");
            return "redirect:/stu/doSearch";
        }else {
            stuService.doIns(student);
            return "redirect:/stu/doSearch";
        }
    }

    @RequestMapping("doUpdate")
    public String doUpdate(Student student){
        stuService.doUpdate(student);
        return "redirect:/stu/doSearch";
    }
}
